package com.example.HelloSpace;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.HelloSpace.database.AppDatabase;
import com.example.HelloSpace.factory.SubscribedMissionsViewModelFactory;
import com.example.HelloSpace.service.MissionGenerator;
import com.example.HelloSpace.ui.CarouselView;
import com.example.HelloSpace.utils.MyApp;
import com.example.HelloSpace.viewmodels.SubscribedMissionsViewModel;
import com.example.HelloSpace.database.entities.SpaceMissionEntity;

import java.util.Arrays;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomePageRecommendFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomePageRecommendFragment extends Fragment {

    private CarouselView carouselView;

    private LinearLayout missionContainer;
    private SubscribedMissionsViewModel viewModel;
    private Observer<List<SpaceMissionEntity>> missionsObserver;

    public HomePageRecommendFragment() {
        // Required empty public constructor
    }

    public static HomePageRecommendFragment newInstance(String param1, String param2) {
        HomePageRecommendFragment fragment = new HomePageRecommendFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_page_recommend, container, false);
        missionContainer = view.findViewById(R.id.home_page_recommend_container);

        AppDatabase db = AppDatabase.getDatabase(getContext());
        // 使用自定义的 ViewModel 工厂
        SubscribedMissionsViewModelFactory factory = new SubscribedMissionsViewModelFactory(requireActivity().getApplication(), db);
        viewModel = new ViewModelProvider(this, factory).get(SubscribedMissionsViewModel.class);

        missionsObserver = spaceMissionEntities -> {
            // 当数据发生变化时，使用Generator来更新UI
            MissionGenerator.generateMissionsView(getContext(), missionContainer, spaceMissionEntities, viewModel);
        };
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        carouselView = view.findViewById(R.id.carouselView);
        String SEVER_URL = getResources().getString(R.string.SERVER_URL);
        // 设置图片URL列表
        List<String> imageUrls = Arrays.asList(
                "/images/test01.jpg",
                "/images/test02.png",
                "/images/test03.jpg"
        );
        carouselView.setImageUrls(imageUrls);

        // 设置自动播放间隔（可选）
        carouselView.setAutoPlayInterval(5000);

        carouselView.setCurrentItem(1, false);
    }
    @Override
    public void onResume() {
        super.onResume();
        // 这里可以继续处理一些逻辑
        MyApp.updateStatusBarColorAndIcons(true, requireActivity());
        carouselView.startAutoPlay();
        viewModel.getSubscribedMissions().observe(getViewLifecycleOwner(), missionsObserver);
    }

    @Override
    public void onPause() {
        super.onPause();
        // 这里可以处理暂停逻辑
        viewModel.getSubscribedMissions().removeObserver(missionsObserver);
    }
}